package com.virtualtrainer_01.dao;

import com.virtualtrainer_01.model.Aliment;
import com.virtualtrainer_01.model.Model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class AlimentDB extends SQLiteDB {
	
	public static final String KEY_ID = "_id";
	public static final String KEY_NAME = "name";
	public static final String KEY_PRICE = "price";
	public static final String KEY_AMOUNT = "amount";
	private static final String DATABASE_TABLE = "aliment";
	public AlimentDB(Context c) {
		super(c, DATABASE_TABLE);
	}

	@Override
	public String getData() {
		String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_PRICE,KEY_AMOUNT };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);
		String result = "";

		int iRow = c.getColumnIndex(KEY_ID);
		int iName = c.getColumnIndex(KEY_NAME);
		int iPrice = c.getColumnIndex(KEY_PRICE);
		int iAmount = c.getColumnIndex(KEY_AMOUNT);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
			result = result + c.getString(iRow) + " " + c.getString(iName)
					+ " " + c.getString(iPrice) + " "+ c.getString(iAmount);
		return result;
	}

	@Override
	public long createEntry(Model entry) {
		Aliment a = (Aliment) entry;
		ContentValues cv = new ContentValues();
		cv.put(KEY_NAME, a.getName());
		cv.put(KEY_AMOUNT, a.getAmount());
		cv.put(KEY_PRICE, a.getPrice());		
		return ourDatabase.insert(DATABASE_TABLE, null, cv);
	}
}
